package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.MinioUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MineVideoMapper;
import com.ruoyi.system.domain.MineVideo;
import com.ruoyi.system.service.IMineVideoService;

/**
 * 视频Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-14
 */
@Service
public class MineVideoServiceImpl implements IMineVideoService 
{
    @Autowired
    private MineVideoMapper mineVideoMapper;

    @Autowired
    private MinioUtil minioUtil;

    private static final Logger log = LoggerFactory.getLogger(MinioUtil.class);

    /**
     * 查询视频
     * 
     * @param videoId 视频主键
     * @return 视频
     */
    @Override
    public MineVideo selectMineVideoByVideoId(Long videoId)
    {
        return mineVideoMapper.selectMineVideoByVideoId(videoId);
    }

    /**
     * 查询视频
     *
     * @param trainingId 视频主键
     * @return 视频
     */
    @Override
    public List<MineVideo> selectMineVideoListByTrainingId(Long trainingId) {
        return mineVideoMapper.selectMineVideoListByTrainingId(trainingId);
    }

    /**
     * 查询视频列表
     * 
     * @param mineVideo 视频
     * @return 视频
     */
    @Override
    public List<MineVideo> selectMineVideoList(MineVideo mineVideo)
    {
        return mineVideoMapper.selectMineVideoList(mineVideo);
    }

    /**
     * 新增视频
     * 
     * @param mineVideo 视频
     * @return 结果
     */
    @Override
    public int insertMineVideo(MineVideo mineVideo)
    {
        return mineVideoMapper.insertMineVideo(mineVideo);
    }

    /**
     * 修改视频
     * 
     * @param mineVideo 视频
     * @return 结果
     */
    @Override
    public int updateMineVideo(MineVideo mineVideo)
    {
        return mineVideoMapper.updateMineVideo(mineVideo);
    }

    /**
     * 批量删除视频
     * 
     * @param videoIds 需要删除的视频主键
     * @return 结果
     */
//    @Override
//    public int deleteMineVideoByVideoIds(Long[] videoIds)
//    {
//        return mineVideoMapper.deleteMineVideoByVideoIds(videoIds);
//    }
    @Override
    public int deleteMineVideoByVideoIds(Long[] videoIds) {
        // 1. 查询所有需要删除的视频记录
        List<MineVideo> videos = mineVideoMapper.selectVideosByIds(videoIds);

        // 2. 删除Minio文件
        videos.forEach(video -> {
            try {
                // 根据trainingId和videoId构造前缀
                String prefix = String.format("training-%d/video-%d/",
                        video.getTrainingId(),
                        video.getVideoId());

                // 调用通用清理方法
                minioUtil.cleanObjects(
                        minioUtil.getVideoBucketName(),
                        prefix
                );
            } catch (Exception e) {
                log.error("删除Minio文件失败 videoId={}: {}", video.getVideoId(), e.getMessage());
                throw new RuntimeException("文件删除失败");
            }
        });

        // 3. 删除数据库记录
        return mineVideoMapper.deleteMineVideoByVideoIds(videoIds);
    }
    /**
     * 删除视频信息
     * 
     * @param videoId 视频主键
     * @return 结果
     */
    @Override
    public int deleteMineVideoByVideoId(Long videoId)
    {
        return mineVideoMapper.deleteMineVideoByVideoId(videoId);
    }
}
